package com.neusoft.neuiotms.dcom.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.neusoft.bizcore.auth.common.bean.UserBean;
import com.neusoft.bizcore.web.dto.result.PageResultDTO;
import com.neusoft.bizcore.web.dto.result.ResultDTO;
import com.neusoft.bizcore.web.dto.result.ResultListDTO;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.dcom.converter.UserAuthorityConditionConverter;
import com.neusoft.neuiotms.dcom.dto.HandoverDTO;
import com.neusoft.neuiotms.dcom.model.enums.HandoverStatus;
import com.neusoft.neuiotms.dcom.service.HandoverService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "交接班管理")
@RestController
@RequestMapping(value = "/api/w/handover")
public class HandoverController {

    @Autowired
    private HandoverService handoverService;
    @Autowired
    private UserAuthorityConditionConverter userAuthorityConditionConverter;

    @ApiOperation("查询所有交接班")
    @GetMapping
    public ResultListDTO<HandoverDTO> index(final Searchable searchable, final UserBean user) {
        this.userAuthorityConditionConverter.addUserDownOrgCodesAuthoritySearchCondition(searchable, user);
        this.userAuthorityConditionConverter.addUserFctAuthoritySearchCondition(searchable, user);
        return ResultListDTO.success(this.handoverService.index(searchable));
    }

    @ApiOperation("分页查询所有交接班")
    @GetMapping("/p")
    public PageResultDTO<HandoverDTO> search(final Pageable pageable, final Searchable searchable,
            final UserBean user) {
        this.userAuthorityConditionConverter.addUserDownOrgCodesAuthoritySearchCondition(searchable, user);
        this.userAuthorityConditionConverter.addUserFctAuthoritySearchCondition(searchable, user);
        return PageResultDTO.success(this.handoverService.search(pageable, searchable));
    }

    @ApiOperation("创建交接班记录")
    @PostMapping("")
    public ResultDTO<HandoverDTO> create(@RequestBody final HandoverDTO dto, final UserBean user) {
        return ResultDTO.success(this.handoverService.create(dto, user));
    }

    @ApiOperation(value = "修改交接班")
    @PutMapping("/{id}")
    public ResultDTO<HandoverDTO> update(@PathVariable final Long id,
            @RequestBody final HandoverDTO dto) {
        return ResultDTO.success(this.handoverService.update(id, dto));
    }

    @ApiOperation(value = "开始交班")
    @PutMapping("/{id}/start")
    public ResultDTO<HandoverDTO> startHandover(@PathVariable final Long id) {
        return ResultDTO.success(this.handoverService.updateStatus(id, HandoverStatus.run));
    }

    @ApiOperation("交接班详情")
    @GetMapping("/{id}")
    public ResultDTO<HandoverDTO> details(@PathVariable final Long id) {
        return ResultDTO.success(this.handoverService.details(id));
    }

    @ApiOperation("删除交接班")
    @DeleteMapping("/{id}")
    public ResultDTO<Void> delete(@PathVariable final Long id) {
        this.handoverService.delete(id);
        return ResultDTO.success();
    }
}
